#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<Windows.h>

#define high 20
#define width 30  //define the size of the screen
#define bricknumber 29  //define the number of bricks


//-------Global variable-------//
      //0-space,1-board,2-ball,3-brick,-1-boundary

int carvans[high][width]={0};  //initialize the screen

int board_x,board_y;  //define the position of the board

int boardwidth;  //define the length of the board

int ball_x,ball_y;  //define the position of the ball

int volicity_x,volicity_y;  //define the speed of the ball

int brick_x[bricknumber],brick_y[bricknumber];  //define bricks;

//------SCREEN-CLEAN FUNCTION---------//
void gotoxy(int x,int y)
{
    HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X=x;
    pos.Y=y;
    SetConsoleCursorPosition(handle,pos);
}
//------------------------------------//

//--------HIDECURSOR FUNCTION---------//
void HideCursor()
{
    CONSOLE_CURSOR_INFO cursor_info={1,0};
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}
//------------------------------------//

//-------------initial data-----------//
void startup()
{
    int i,j;

    boardwidth=8;
    board_x=high-2;
    board_y=width/2;
    carvans[board_x][board_y]=1;  //initialize the board

    ball_x=board_x-1;
    ball_y=board_y;
    carvans[ball_x][ball_y]=2;  //initialize the ball

    volicity_x=1;
    volicity_y=1;  //initialize the speed

    for(i=0;i<high;i++)
    {
        carvans[i][0]=-1;
        carvans[i][width-1]=-1;
    }

    for(j=0;j<width;j++)
    {
        carvans[0][j]=-1;
        carvans[high-1][j]=-1;
    }  //define the boundary

    for(i=0;i<=bricknumber;i++)
        brick_x[i]=1;
    for(j=1;j<bricknumber;j++)
        brick_y[j]=j;

    for(i=1;i<bricknumber;i++)
    {
        carvans[brick_x[i]][brick_y[i]]=3;
    }

    HideCursor();  //hide the cursor
}
//------------------------------------//

//-----------SHOW FUNCTION------------//
void show()
{
    gotoxy(0,0);  //clear the screen

    int i,j,temp;
    for(i=0;i<high;i++)
    {
        for(j=0;j<width;j++)
        {
            if(carvans[i][j]==0)
               printf(" ");  //output the interface

            if(carvans[i][j]==1)
            {
                temp=j;

                for(j=temp-1;j<=temp+boardwidth;j++)
                    printf("*");
            }  //output the board

            if(carvans[i][j]==-1)
                printf("#");  //output the boundary

            if(carvans[i][j]==2)
                printf("o");  //output the ball
            if(carvans[i][j]==3)
                printf("E");
        }
        printf("\n");

    }
}
//------------------------------------//

//--------UPDATE WITHOUT INPUT--------//
void updateWithoutInput()
{
    static int speed=10;
    int i,j;
    if(speed<=20)
        speed++;
    if(speed>20)
    {
        carvans[ball_x][ball_y]=0;
        ball_x=ball_x-volicity_x;
        ball_y=ball_y-volicity_y;  //control the movement of the ball
        carvans[ball_x][ball_y]=2;  //output the ball
        speed=10;
    }

    if(ball_x==high-2||ball_x==1)
        volicity_x=-volicity_x;
    if(ball_y==1||ball_y==width-2)
        volicity_y=-volicity_y;
    if((ball_x==board_x-1)&&(ball_y>board_y)&&(ball_y<board_y+boardwidth+2))
        volicity_x=-volicity_x;  //change the direction

    for(i=0;i<high;i++)
    {
        for(j=0;j<width;j++)
        {

        }
    }


}
//------------------------------------//

//----------UPDATE WITH INPUT---------//
void updateWithInput()
{
    char input;

    if(kbhit())
    {
        input=getch();

        if(input=='a')
        {
            carvans[board_x][board_y]=0;
            board_y--;
            carvans[board_x][board_y]=1;
        }
        else if(input=='d')
        {
            carvans[board_x][board_y]=0;
            board_y++;
            carvans[board_x][board_y]=1;
        }


    }

}  //reaction toward input
//------------------------------------//

//------------MAIN FUNTION------------//
int main()
{
	startup();
	while(1)
	{
		show();
		updateWithInput();
		updateWithoutInput();
	}
	return 0;
}
//------------------------------------//
